#!/usr/bin/python
import sqlite3
import os
import copy
import sys

#check for events to send

conn = sqlite3.connect('/var/mobile/dmassms.db')

c = conn.cursor()
number = sys.argv[1]
name = sys.argv[2]
groups = []
i=0
for arg in sys.argv:
	if i in (0,1,2):
		i = i + 1
	else:
		groups.append(arg)
		i = i + 1


#c.execute("select * from sendnowmobiles;")

#sendlist = []
#for row in c:
#	sendlist.append(copy.deepcopy(row))

#for row in sendlist:
#	command = "smssend " + row[5] + " \"(massms) *test* " + 
#row[3].replace("\\","\\\\").replace("\"","\\\"") + "\""
#	print(command)
#	os.system(command)
#	c.execute("insert into sent(alertid,numberid,senttime) select " 
#+ str(row[0]) + "," + str(row[4]) + ",datetime('now','localtime');")
c.execute("insert into numbers(number,dateadded,name) select '" + number+ "', datetime('now','localtime'), '" + name + "';") 

for group in groups:
	c.execute("insert into numberstogroups(numberid,groupid) select numberid,groupid from numbers,groups where number = '" + number + "' and groupname = '" + group + "';")

conn.commit()

os.system("smssend " + number + " \"(massms) You have been added to the txt message list.\"")	

	
	
	



